package DBOpt;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.*;

public class ResultProcessor {
	private HashSet<String> KnownErrorSet;
	
	protected ResultProcessor()
	{
		KnownErrorSet = new HashSet<String> ();
		KnownErrorSet.add("File Duplicate");
		KnownErrorSet.add("Records Duplicate");
		KnownErrorSet.add("Empty XML file");
		KnownErrorSet.add("XML Error");
		KnownErrorSet.add("UNKNOW Error");
	}
	
	private void process(String resultFile) throws Exception
	{
		  FileWriter outputfile = new FileWriter ("..//output/meas/ErrorResult.txt",true);
		  System.out.println("creat outpufile file");
		  outputfile.write("***  Start ***\n");
		  FileReader fr = new FileReader(resultFile);
		  BufferedReader br = new BufferedReader(fr);
		  String line;
		  TreeMap <String,Integer> typemap = new TreeMap <String,Integer> ();
		  String current_date = "";
		   while(true)
	       {
	    	   line = br.readLine();
	    	   if (line == null) break;
	           if (line.length()==0) continue;
	           int tabpos = line.indexOf('\t');
	           if (tabpos <=0) continue;
	           String filename = line.substring(0, tabpos).trim();
	           String errormsg = line.substring(tabpos,line.length()).trim();
	           
	           if (!filename.matches(".*2008[0-9][0-9][0-9][0-9].*")) continue;
	           System.out.println("file: "+filename+"error:"+errormsg);
	           String date = filename.substring(filename.indexOf("2008"),filename.indexOf("2008")+8);
	           System.out.println("date:--"+date+"--");
	           //System.out.println("current_date:--"+current_date+"--");
	           if (current_date == "" ) // first day
	           {
	        	   current_date = date;
	           }
	           else if (! (current_date.equals(date))) // a new day
	           {
	        	   //output previous day
	        	   
	        	    Iterator<Map.Entry<String, Integer>> it = typemap.entrySet().iterator();
	        	    while (it.hasNext()) {
	        	        Map.Entry<String, Integer> pairs = it.next();
	        	        System.out.println(pairs.getKey() + " = " + pairs.getValue());
	        	        outputfile.write(current_date+"\t"+pairs.getKey()+"\t"+pairs.getValue()+"\n");
	        	        
	        	    }
	        	    outputfile.write("\n");
        	        outputfile.flush();
	               current_date = date; 
	        	   typemap.clear();
	           }
	           String type;
        	   int recordnum = 1;
        	   if (errormsg.indexOf("Records Duplicate") != -1)
        	   {
        		   System.out.println("num---"+errormsg.trim().substring(0,errormsg.indexOf(" ")));
        		   recordnum = Integer.parseInt(errormsg.substring(0,errormsg.indexOf(" ")));
        		   
        		   System.out.println("recordnum: "+recordnum);
        		   type = "Records Duplicate";
        		   
        	   }
        	   else if (errormsg.indexOf("UNKNOW Error")  == 0)
        		   type = "UNKNOW Error";
        	   else if (errormsg.indexOf("XML Error")  == 0)
        		   type = "XML Error";
        	   else if (errormsg.indexOf("Exec SQL Error")  == 0)
        		   type = "Exec SQL Error";

        	   else {type = errormsg;}
        	   System.out.println("type: "+type);
        	
        	   if (typemap.containsKey(type))
        		 {
        		   int previousvalue = typemap.get(type); 
        		   typemap.put(type,previousvalue+recordnum);
        		  }
        	   else
        	   {
        		   typemap.put(type,recordnum);
        	   }
        		
	       }
		  
		   //output the last day
		   if (current_date != null )
		   {
			   Iterator<Map.Entry<String, Integer>> it = typemap.entrySet().iterator();
		   	    while (it.hasNext()) {
		   	        Map.Entry<String, Integer> pairs = it.next();
		   	        System.out.println(pairs.getKey() + " = " + pairs.getValue());
		   	        outputfile.write(current_date+"\t"+pairs.getKey()+"\t"+pairs.getValue()+"\n");
		   	        outputfile.flush();
		   	    }
		   }   
		outputfile.close();
	}
	
	public static void main(String args[]) {
		   
		if (args.length < 1)
		{
			System.out.println("Usage: resultProcessor <result file>");
			System.exit(0) ;
		}
		try{  
		String resultFile = args[0];
		
		
		ResultProcessor resultProcessor = new ResultProcessor();
		resultProcessor.process( resultFile);
		}
	      catch( Exception e ) 
	      {
	         e.printStackTrace();
	       }
	      System.out.println("Done!");
	  
	}


}
